//
//  LHAppUpdateManager.m
//  LHAppUpdateManager
//
//  Created by 美食家 on 18/1/2.
//  Copyright © 2018年 不孤独的美食家. All rights reserved.
//

#import "LHAppUpdateManager.h"

#import "LHNetWorkManager.h"
#import "NSString+version.h"
/** 根据应用的APPID从App Store获取应用信息 */
#define appIDUrl(appid) [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appid];
/** 根据应用的bundleID从App Store获取应用信息 */
#define bundleIDUrl(bundelId) [NSString stringWithFormat:@"https://itunes.apple.com/lookup?bundleId=%@",bundelId];

@implementation LHAppUpdateManager

+(void)lh_checkAppUpdateInfoWithAppID:(NSString*)AppID andRequestMethod:(NSString*)method andSuccess:(void(^)(BOOL isNewerVersion,NSString*updatedurl,NSString*updateText))success andFailure:(void(^)(NSError* error))failure{
    
    NSString*url=appIDUrl(AppID);
    [LHNetWorkManager lh_startRequestWithUrl:url andRequestMethod:method andSuccess:^(NSDictionary * _Nullable data) {
        NSLog(@"data=%@",data);
        
        if ([[NSString lh_getApplicationVersion] isEqualToString:data[@"version"]]) {
            NSLog(@"当前版本为最新版本,无更新文本");
            success(NO,@"",@"");
        }else{
            NSLog(@"当前版本不是最新版本，需要更新");
            success(YES,data[@"trackViewUrl"],data[@"description"]);
        }
    } andFailure:^(NSError * _Nullable error) {
        if (error) {
            failure(error);
        }
    }];
    
}
@end

